package com.lvgang.springbootrabbitmq.publishsubscribe;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.UUID;

/**
 * @author lvgang
 */
@Component
public class PublishSubscribeSender {
	private static Logger logger = LoggerFactory.getLogger(PublishSubscribeSender.class);
	@Autowired
	private RabbitTemplate rabbitTemplate;
	public void send(int i) {
		CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
		String content = "ExchangeFanout="+ i+"," + new Date() + ", content= " + UUID.randomUUID().toString();

		//消息发送，使用void convertAndSend(String exchange, String routingKey, Object message) throws AmqpException;
		//但不指定routingKey。因为FanoutExchange类型的交换机，routingKey不起作用，它向所有的队列发送广播，只要队列绑定到该交换机即接受消息。
		this.rabbitTemplate.convertAndSend(RabbitPublishSubscribeConstant.EXCHANGE,"",content,correlationData);
		logger.info("Send ok,"+new Date()+","+content);
	}
}